<?php if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Taxonomy
 * 
 * @package CMS Project
 * @author Tùng Nguyễn
 * @copyright 2012
 * @version 1.0
 * @access public
 */


class Taxonomy extends CI_Controller
{
    public function __construct()
    {
        session_start();
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library(array('form_validation', 'other'));
        $this->load->dbforge();
        $this->load->database();
        $this->load->model('m_taxonomy');
    }


    public function index()
    {
        $data['data'] = $this->m_taxonomy->getAll();
        $this->load->view('header');
        $this->load->view('taxonomy/v_index', $data);
        $this->load->view('footer');
    }
  public function test()
  {
  echo "ok";
  }

    public function create()
    {

        $data['message'] = null;
        $this->form_validation->set_rules('value_vocabulary', 'Value Vocabulary',
            'required|callback_checkTable');
        $this->form_validation->set_rules('vocabulary', 'Name Vocabulary',
            'trim|required');
        if ($this->form_validation->run() == false) {
            $this->load->view('header');
            $this->load->view('taxonomy/v_create');
            $this->load->view('footer');
        } else {

            $name_vocabulary = $this->input->post('vocabulary');
            $value_vocabulary = strtolower($this->input->post('value_vocabulary'));

            $arrayData = array('nameTaxonomy' => $name_vocabulary, 'valueTaxonomy' => $value_vocabulary);
            $this->m_taxonomy->create($arrayData, $value_vocabulary);
            redirect('taxonomy');
        }

    }

    public function checkTable($str)
    {
        if ($this->db->table_exists($str) == true) {
            $this->form_validation->set_message('checkTable', 'Bảng này đã tồn tại');
            return false;
        } else {
            return true;
        }
    }

    public function update()
    {


    }


    public function delete()
    {


    }


    public function read()
    {


    }

    /**
     * Create Term
     **/
    public function createTerm($table, $gencode = null)
    {
        // Parkage 
        $project = $this->other->project;
        $author  = $this->other->author;
        $version = $this->other->version;
        if (isset($gencode) == 'gencode') {
            // create folder
            mkdir('application/modules/' . $table);
            mkdir('application/modules/' . $table . '/controllers/');
            mkdir('application/modules/' . $table . '/models/');
            mkdir('application/modules/' . $table . '/views/');
            // create filde
            $source_controller = fopen("application/modules/" . $table . "/controllers/" . $table . ".php", "w+");

            $source_model      = fopen("application/modules/" . $table . "/models/m_" . $table . ".php", "w+");

            fopen("application/modules/" . $table . "/views/v_create.php", "w+");
            fopen("application/modules/" . $table . "/views/v_delete.php", "w+");
            fopen("application/modules/" . $table . "/views/v_read.php", "w+");
            fopen("application/modules/" . $table . "/views/v_update.php", "w+");
            fopen("application/modules/" . $table . "/views/v_index.php", "w+");

            // Get Info Field
            $field = $this->db->field_data($table);
            
            // important content
            $content_controller1 = "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class $table extends CI_Controller {

    /**
     * Constructor Param Term
     * 
     * @Project     $project
     * @Module   	$table
     * @author		$author
     * @param:\n";
            fwrite($source_controller, $content_controller1);
            fclose($source_controller);
            $append_controller = fopen("application/modules/" . $table . "/controllers/$table.php", "a+");
            foreach ($field as $s_field) {
                if ($s_field->primary_key) {
                    fwrite($append_controller, "     * - " . $s_field->name . "   | Type:" . $s_field->type . " | Length:" . $s_field->max_length . " | Primary Key : TRUE \n");
                } elseif ($s_field->default != '') {
                    fwrite($append_controller, "     * - " . $s_field->name . "          | Type:" . $s_field->type . " | Length:" . $s_field->max_length . " | Default :" . $s_field->default .
                        " \n");
                } else {
                    fwrite($append_controller, "     * - " . $s_field->name . "          | Type:" . $s_field->type . " | Length:" . $s_field->max_length . "\n");
                }

            }
            $last_append_controller = "     */
 
    public function __construct()
    {
        parent::__construct();
        \$this->load->database();
        \$this->load->helper(array(''));
        \$this->load->library(array(''));
        \$this->load->model('m_$table');
    }
    
	public function index()
	{
		\$this->load->view('view');
	}
    
    public function update()
    {


    }


    public function delete()
    {


    }


    public function read()
    {


    }
}

/* End of file $table.php */";

fwrite($append_controller,$last_append_controller);
fclose($append_controller);

$content_model ="<?php
/**
 * File name : m_$table
 * 
 * @package $project
 * @author $author
 * @copyright 2012
 * @version $version
 */
class m_$table extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function insert(\$dataArray)
    {
        \$this->db->insert('$table',\$dataArray);
    }
    
    
    public function update(\$id,\$dataArray)
    {
        \$this->db->where('id$table',\$id);
        \$this->db->update('$table',\$dataArray);
    } 
    
    public function delete(\$id)
    {
        \$this->db->delete('$table',array('id$table'=>\$id));
    }
     
    public function getData(\$id = '')
    {
        if(isset(\$id) == '')
        {
            \$query = \$this->db->get('$table');
            return \$query->result_array();            
        }else{
            \$query = \$this->db->get_where('$table',array('id$table' => \$id));
        }

    }
}
?>                
";
fwrite($source_model,$content_model);
fclose($source_model);

redirect('taxonomy');
        }
        if (isset($table) == null) {
            redirect("taxonomy");
        } else {
            if ($this->db->table_exists($table) == true) {
                $data['vocabulary'] = $table;

                $this->form_validation->set_rules('name_term', 'Name Term', 'required|trim');
                $this->form_validation->set_rules('value_term', 'Value Term', 'required|trim');
                if ($this->form_validation->run() == false) {

                    $this->load->view('header');
                    $this->load->view("term/v_create", $data);
                    $this->load->view('footer');
                } else {

                    $name_term = $this->input->post('name_term');
                    $value_term = $this->input->post('value_term');
                    $data['name_term'] = $name_term;
                    $data['value_term'] = $value_term;

                    $this->load->view('header');
                    $this->load->view("term/v_create", $data);
                    $this->load->view('footer');
                }
            } else {
                $data['message'] = "Không có dữ liệu";
                $this->load->view('header');
                $this->load->view("term/v_create", $data);
                $this->load->view('footer');
            }
        }
    }
}

?>